
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class SizingWindow implements ActionListener, ItemListener
{
	JFrame sizeWin = new JFrame("Connect 4");
	JTextField Rows = new JTextField(4);
	JTextField Columns = new JTextField(4); 
	JTextField Time = new JTextField(4);
	JCheckBox Flip = new JCheckBox("Allow Board Flipping");
	JCheckBox Remove = new JCheckBox("Allow Removal of Bottom Piece");
	
	Boolean FlipSet=false;
	Boolean RemoveSet=false;
	JButton start=new JButton("Start");
	JButton host = new JButton("Host");
	JButton connect= new JButton("Connect");
	
	Boolean ishost=false;
	Boolean isserver=false;
	
	
	public SizingWindow()
	{
		//set parameters
		sizeWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//Buttons etc.
		Container content=sizeWin.getContentPane();
		content.setLayout(new BoxLayout(sizeWin.getContentPane(),BoxLayout.PAGE_AXIS));
		JLabel lblrows = new JLabel("Rows");
		JLabel lblcolumns = new JLabel("Columns");
		JLabel lbltime = new JLabel("Time");
				
		//ActionListener
		start.addActionListener(this);
		host.addActionListener(this);
		connect.addActionListener(this);
		
		//Creating Check Boxes
		Flip.setMnemonic(KeyEvent.VK_G); 
	    Flip.setSelected(false);
	    
	    Remove.setMnemonic(KeyEvent.VK_G); 
	    Remove.setSelected(false);
	    
	    //Item listener
	    Flip.addItemListener(this);
	    Remove.addItemListener(this);
	    
				
		//Display Window and attach widgets
		sizeWin.setSize(400,400);
		sizeWin.setLocationRelativeTo(null);
		
		Time.setAlignmentX(Component.CENTER_ALIGNMENT);
		lbltime.setAlignmentX(Component.CENTER_ALIGNMENT);
		
		lblrows.setAlignmentX(Component.CENTER_ALIGNMENT);
		lblcolumns.setAlignmentX(Component.CENTER_ALIGNMENT);
		
		Rows.setMaximumSize(Rows.getPreferredSize());
		Columns.setMaximumSize(Columns.getPreferredSize());
		Time.setMaximumSize(Time.getPreferredSize());
		
		Flip.setAlignmentX(Component.CENTER_ALIGNMENT);
		Remove.setAlignmentX(Component.CENTER_ALIGNMENT);
		
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
		buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
		buttonPane.add(start);
		buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
		buttonPane.add(host);
		buttonPane.setAlignmentX(Component.CENTER_ALIGNMENT);

		
		//add widgets to panel
		
		content.add(lblrows);
		content.add(Rows);
		content.add(lblcolumns);
		content.add(Columns);
		content.add(lbltime);
		content.add(Time);
		content.add(Flip);
		content.add(Remove);
		content.add(connect);
		content.add(buttonPane,BorderLayout.AFTER_LAST_LINE);
	
		sizeWin.setVisible(true);
	}
	//Go button clicked event
	public void actionPerformed(ActionEvent action) 
	{
		String namestr="";
		String portstr="";
		if(action.getSource()==start)
		{
			//get width and height from text fields
			int rows = 0;
			int columns = 0;
			int time = 0;
			Boolean caught = false;
			
			//Make sure that input is indeed an integer
			try
			{
				rows = Integer.parseInt(Rows.getText());
				columns = Integer.parseInt(Columns.getText());
				
			}
			catch(NumberFormatException e)
			{
				JOptionPane.showMessageDialog(null,"The default size will be used.");
				sizeWin.dispose();
				caught = true;
				
			    //create game window
				new ConnectFourLauncher(7,6,75,FlipSet,RemoveSet,0,isserver,ishost,portstr,namestr);
			}
			try
			{
				time = Integer.parseInt(Time.getText());
			}
			catch(NumberFormatException e)
			{
				time=0;
			}
			//both dimensions must =4
			if((rows<4 || columns<4) && !caught )
			{
				JOptionPane.showMessageDialog(null,"The game board must contain at least four rows and four colums.",
						"Error",JOptionPane.ERROR_MESSAGE);
			}
			else if(!caught)
			{
				sizeWin.dispose();
				//create game window
				new ConnectFourLauncher(columns,rows,75,FlipSet,RemoveSet,time,isserver,ishost,portstr,namestr);
			}
		}
		if(action.getSource()==host)
		{
			JTextField name = new JTextField();
			JTextField port = new JTextField();
			Object[] msg = {"Server", name, "Port", port};
			Object[] options = {"Connect"};
	 
			JOptionPane op = new JOptionPane(
				msg,
				JOptionPane.QUESTION_MESSAGE,
				JOptionPane.OK_OPTION,
				null,
				options);
	 
			JDialog dialog = op.createDialog("Game Hosting");
			dialog.setVisible(true);
			namestr=name.getText();
			portstr=port.getText();
		}
		if(action.getSource()==connect)
		{
			JTextField name = new JTextField();
			JTextField port = new JTextField();
			Object[] msg = {"Server", name, "Port", port};
			Object[] options = {"Start"};
	 
			JOptionPane op = new JOptionPane(
				msg,
				JOptionPane.QUESTION_MESSAGE,
				JOptionPane.OK_OPTION,
				null,
				options);
	 
			JDialog dialog = op.createDialog("Game Hosting");
			dialog.setVisible(true);
			namestr=name.getText();
			portstr=port.getText();
		}
	}
	public void itemStateChanged(ItemEvent e) 
	{
		Object box = e.getItemSelectable();
		if(box == Flip)
		{
			FlipSet = true;
		}
		if(box == Remove)
		{
			RemoveSet = true;
		}
		if(e.getStateChange() == ItemEvent.DESELECTED)
		{
			if(box == Flip)
			{
				FlipSet = false;
			}
			if(box == Remove)
			{
				RemoveSet = true;
			}
		}
		
		
	}
}
